home *** CD-ROM | disk | FTP | other *** search
/ Aminet 19 / Aminet 19 (1997)(GTI - Schatztruhe)[!][Jun 1997].iso / Aminet / gfx / board / rtgmasdev.lha / demos / mandel / mandel.c < prev    next >
C/C++ Source or Header  |  1997-03-24  |  8KB  |  294 lines

  1. /*
  2.  
  3.         RTG Library Usage Demo - MandelBrot Generator
  4.  
  5. */
  6. //* "Includes"
  7.  
  8.  
  9. /* Generic "Include Everything"-type file */
  10. #include <math.h>
  11. #include <stdlib.h>
  12. #include <proto/utility.h>
  13. #include <proto/exec.h>
  14. #include <clib/rtgmaster_protos.h>
  15. #include <clib/exec_protos.h>
  16. #include <clib/utility_protos.h>
  17. #include <pragmas/rtgmaster_pragmas.h>
  18. #include <pragmas/exec_pragmas.h>
  19. #include <pragmas/utility_pragmas.h>
  20. #include <exec/memory.h>
  21. #include <utility/tagitem.h>
  22. #include <rtgmaster/rtgmaster.h>
  23. #include <rtgmaster/rtgsublibs.h>
  24. #include <rtgmaster/rtgc2p.h>
  25. #include <proto/rtgmaster.h>
  26. #include "timer.h"
  27. #include <string.h>
  28. #include <stdio.h>
  29.  
  30.  
  31. //*
  32. //* "Globals"
  33. struct RtgScreen *RtgScreen;
  34. struct ScreenReq *sr;
  35. struct RTGMasterBase *RTGMasterBase;
  36. struct Library *UtilityBase, *IntuitionBase;
  37. struct Library *GfxBase;
  38. struct TagItem rtag[] = {
  39.     smr_ChunkySupport,  -1,
  40.     smr_PlanarSupport,  0,
  41.     smr_PrefsFileName,  (Tag)"MandelRTG.prefs",
  42.     TAG_DONE,           NULL
  43. };
  44.  
  45. struct TagItem gtag[] = {
  46.     grd_BytesPerRow,    0,
  47.     grd_Width,          0,
  48.     grd_Height,         0,
  49.     grd_Depth,          0,
  50.     grd_PixelLayout,    0,
  51.     grd_ColorSpace,     0,
  52.     grd_PlaneSize,      0,
  53.     grd_BusSystem,      0,
  54.     TAG_DONE,           0
  55. };
  56.  
  57. struct TagItem tacks[] = {
  58.     TAG_DONE,0
  59. };
  60.  
  61. BOOL Planar;
  62. UBYTE *cbuf=NULL;
  63. ULONG width;
  64. UBYTE *sadr;
  65. ULONG cmap[800];
  66. ULONG size;
  67. #define CBUF 76800
  68.  
  69.  
  70. typedef struct {
  71.     float x,y;
  72. } complex;
  73.  
  74.  
  75. const MAX=256;
  76.  
  77. /*
  78.  * This is done using an ugly direct hardware hit
  79.  * Should be an input handler or similar in future versions/real-life
  80.  * applications!
  81.  */
  82. extern int MouseButton(void);
  83.  
  84. //*
  85. //* "fail"
  86. void fail(void) {
  87.     if (RtgScreen) CloseRtgScreen(RtgScreen);
  88.     if (RTGMasterBase) CloseLibrary((struct Library *)RTGMasterBase);
  89.     if (UtilityBase) CloseLibrary(UtilityBase);
  90.     if (cbuf) FreeVec(cbuf);
  91.     exit(0L);
  92. }
  93. //*
  94. //* "Iterate"
  95. int iterate(complex zInit) {
  96.     complex z;
  97.     float a,b;
  98.     int cnt;
  99.  
  100.     z=zInit;
  101.     cnt=0;
  102.     while ((z.x*z.x+z.y*z.y<=4.0) && (cnt<MAX)) {
  103.         a=z.x*z.x-z.y*z.y;
  104.         b=2*z.x*z.y;
  105.         z.x=a; z.y=b;
  106.         z.x+=zInit.x;
  107.         z.y+=zInit.y;
  108.         cnt++;
  109.     }
  110.     return cnt;
  111. }
  112. //*
  113. //* "Mandelbrot"
  114. void Mandelbrot(float rMin, float rMax, float iMin, float iMax,
  115.     int cols, int rows) {
  116.     float rInc, iInc;
  117.     int x,y,count;
  118.     complex zInit;
  119.  
  120.     rInc=(rMax-rMin)/(float)cols;
  121.     iInc=(iMax-iMin)/(float)rows;
  122.     zInit.x=rMin;
  123.     for (x=0; x<cols; x++) {
  124.         zInit.y=iMin;
  125.         for (y=0; y<cols; y++) {
  126.             count=iterate(zInit);
  127.             WriteRtgPixel(RtgScreen,sadr,x,y,count);
  128.             zInit.y+=iInc;
  129.             if (MouseButton()) return;
  130.         }
  131.         zInit.x+=rInc;
  132.     }
  133. }
  134. //*
  135. //* "main"
  136. void main(int argc, char *argv[]) {
  137.    /*
  138.     * Since this is a demo, I don't check anything at all
  139.     * and simply assume that every open went ok... 8-)
  140.     */
  141.     int i,x;
  142.     struct TagItem *tag;
  143.     UBYTE rr, rg, rb;
  144.     ULONG width, height;
  145.     float iMin, iMax, rMin, rMax;
  146.     rMin=-2.25; rMax=0.75;
  147.     iMin=-1.25; iMax=1.25;
  148.  
  149.     if (argc>1) rMin=atof(argv[1]);
  150.     if (argc>2) rMax=atof(argv[2]);
  151.     if (argc>3) iMin=atof(argv[3]);
  152.     if (argc>4) iMax=atof(argv[4]);
  153.  
  154.  
  155.     RTGMasterBase = (struct RTGMasterBase *)OpenLibrary((STRPTR)"rtgmaster.library", 0);
  156.     UtilityBase = OpenLibrary((STRPTR)"utility.library", 37L);
  157.     IntuitionBase = OpenLibrary("intuition.library", 37L);
  158.     GfxBase = OpenLibrary("graphics.library", 37L);
  159.  
  160.     sr = RtgScreenModeReq(rtag);
  161.  
  162.     if (sr==NULL) fail();
  163.  
  164.     RtgScreen = OpenRtgScreen(sr, tacks);
  165.  
  166.     GetRtgScreenData(RtgScreen, gtag);
  167.  
  168.     tag=FindTagItem(grd_BytesPerRow, gtag);
  169.     size = tag->ti_Data;
  170.  
  171.     tag=FindTagItem(grd_Width, gtag);
  172.     width = tag->ti_Data;
  173.  
  174.     tag=FindTagItem(grd_PixelLayout, gtag);
  175.     if (tag->ti_Data != grd_PLANAR && tag->ti_Data != grd_CHUNKY) {
  176.         printf("Screenmode not supported\n");
  177.         fail();
  178.     }
  179.  
  180.     printf("Screen pixel layout is ");
  181.     switch(tag->ti_Data) {
  182.         case grd_PLANAR:        printf("planar\n"); break;
  183.         case grd_PLANATI:       printf("interleaved planar\n"); break;
  184.         case grd_CHUNKY:        printf("8-Bit Z-Ordered (chunky)\n"); break;
  185.         case grd_HICOL15:       printf("15-Bit Chunky (2 Byte/pixel)\n"); break;
  186.         case grd_HICOL16:       printf("16-Bit Chunky (2 Byte/pixel)\n"); break;
  187.         case grd_TRUECOL24:     printf("24-Bit Chunky (3 Byte/pixel)\n"); break;
  188.         case grd_TRUECOL24P:    printf("24-Bit Chunky (3 Byteplanes/pixel)\n"); break;
  189.         case grd_TRUECOL32:     printf("24-Bit Chunky (4 Bytes/pixel)\n"); break;
  190.         case grd_GRAFFITI:      printf("Graffiti 8 bit\n"); break;
  191.         case grd_TRUECOL32B:    printf("24-Bit Chunky (4 Bytes/pixel)\n"); break;
  192.         default:                printf("unknown (%d)\n", tag->ti_Data); break;
  193.     }
  194.  
  195.     tag=FindTagItem(grd_ColorSpace, gtag);
  196.     if (tag->ti_Data) {
  197.         printf("Color space is ");
  198.         switch(tag->ti_Data) {
  199.             case grd_Palette:   printf("CLUT-Based\n"); break;
  200.             case grd_RGB:       printf("RGB (low-endian RGB)\n"); break;
  201.             case grd_BGR:       printf("BGR (high-endian RGB)\n"); break;
  202.             default:            printf("unknown (%d)\n", tag->ti_Data); break;
  203.         }
  204.     }
  205.  
  206.     tag=FindTagItem(grd_BusSystem, gtag);
  207.     if (tag->ti_Data) {
  208.         printf("Graphics card bus is ");
  209.         switch(tag->ti_Data) {
  210.             case grd_Z3:        printf("Zorro III\n"); break;
  211.             case grd_Z2:        printf("Zorro II\n"); break;
  212.             case grd_Custom:    printf("default custom chips\n"); break;
  213.             case grd_RGBPort:   printf("conneted to the RGB Port\n"); break;
  214.             case grd_GVP:       printf("GVP Special Bus (EGS110)\n"); break;
  215.             case grd_DDirect:   printf("DracCo® Direct\n"); break;
  216.             default:            printf("an unknown bus system\n"); break;
  217.         }
  218.     }
  219.  
  220.     if (tag->ti_Data == grd_PLANAR) Planar = TRUE;
  221.     else Planar = FALSE;
  222.  
  223.     tag=FindTagItem(grd_Width, gtag);
  224.     if (tag) width=tag->ti_Data;
  225.     tag=FindTagItem(grd_Height, gtag);
  226.     if (tag) height=tag->ti_Data;
  227.  
  228.     printf("Screen is %ld x %ld x %ld\n", width, height, gtag[3].ti_Data);
  229.     printf("It has %ld bytes per row\n", size);
  230.  
  231.  
  232.     if (Planar == TRUE) {
  233.         cbuf = AllocVec(CBUF, MEMF_CLEAR|MEMF_FAST);
  234.         if (cbuf==NULL) {
  235.             cbuf=AllocVec(CBUF, MEMF_CLEAR);
  236.             if (cbuf==NULL) {
  237.                 printf("Out of memory *SIGH*\n");
  238.                 fail();
  239.             }
  240.         }
  241.     }
  242.  
  243.     cmap[0] = 256 * 65536;
  244.     rr = 0;
  245.     rg = 0;
  246.     rb = 0;
  247.     x = 1;
  248.     for (i = 0; i < 64; i++) {
  249.         cmap[x++] = rr * 0x1111111;
  250.         cmap[x++] = rg * 0x1111111;
  251.         cmap[x++] = rb * 0x1111111;
  252.         rr += 3;
  253.     }
  254.     for (i = 0; i < 127; i++) {
  255.         cmap[x++] = rr * 0x1111111;
  256.         cmap[x++] = rg * 0x1111111;
  257.         cmap[x++] = rb * 0x1111111;
  258.         rg += 3;
  259.     }
  260.     for (i = 0; i < 60; i++) {
  261.         cmap[x++] = rr * 0x1111111;
  262.         cmap[x++] = rg * 0x1111111;
  263.         cmap[x++] = rb * 0x1111111;
  264.         rb += 3;
  265.     }
  266.     for (i = 0; i < 4; i++) {
  267.         cmap[x++]=0xFFFFFFFF;
  268.         cmap[x++]=0xFFFFFFFF;
  269.         cmap[x++]=0xFFFFFFFF;
  270.     }
  271.     cmap[x]=0;
  272.     LockRtgScreen(RtgScreen);
  273.     LoadRGBRtg(RtgScreen, (APTR) cmap);
  274.     UnlockRtgScreen(RtgScreen);
  275.  
  276.     if (RtgScreen) {
  277.         LockRtgScreen(RtgScreen);
  278.         sadr = (UBYTE *)GetBufAdr(RtgScreen,0);
  279.  
  280.         //Mandelbrot(-1.08816, -0.93391, 0.36621, 0.25723, width, height);
  281.         Mandelbrot(rMin, rMax, iMin, iMax, width, height);
  282.         while (MouseButton()==0);
  283.         UnlockRtgScreen(RtgScreen);
  284.         CloseRtgScreen(RtgScreen);
  285.     }
  286.  
  287.     CloseLibrary(GfxBase);
  288.     CloseLibrary((struct Library *)RTGMasterBase);
  289.     CloseLibrary(UtilityBase);
  290.     CloseLibrary(IntuitionBase);
  291. }
  292. //*
  293.  
  294.